package com.aaa.forum.controller;

import com.aaa.forum.common.AppResult;
import com.aaa.forum.common.ResultCode;
import com.aaa.forum.config.AppConfig;
import com.aaa.forum.model.Message;
import com.aaa.forum.model.User;
import com.aaa.forum.services.IMessageService;
import com.aaa.forum.services.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Api(tags = "私信接口")
@Slf4j
@RestController
@RequestMapping("/message")
public class MessageController {

    @Resource
    private IMessageService messageService;

    @Resource
    private IUserService userService;

    @ApiOperation("发送私信")
    @PostMapping("/send")
    public AppResult send(HttpServletRequest request,
                          @ApiParam("接收者id") @RequestParam("receiveUserId") Long receiveUserId,
                          @ApiParam("内容") @RequestParam("content") String content) {
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        // 1. 判断当前登录用户是否被禁言
        if (user.getState() == 1) {
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        // 2. 接收者id不能是自己的id
        if (user.getId() == receiveUserId) {
            return AppResult.failed("不能给自己发送私信");
        }
        // 3. 判断接收用户是否存在
        User receiveUser = userService.selectById(receiveUserId);
        if (receiveUser == null || receiveUser.getDeleteState() == 1) {
            return AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        // 4. 封装对象
        Message message = new Message();
        message.setPostUserId(user.getId());
        message.setReceiveUserId(receiveUser.getId());
        message.setContent(content);
        // 5. 调用 service
        messageService.create(message);
        // 6. 返回结果
        return AppResult.success("发送成功");
    }

    @ApiOperation("获取未读私信数")
    @GetMapping("/getUnreadCount")
    public AppResult<Integer> getUnreadCount(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        Integer count = messageService.selectUnreadCount(user.getId());
        return AppResult.success(count);
    }

    @ApiOperation("获取当前用户所有私信")
    @GetMapping("/getAll")
    public AppResult<List<Message>> getAll(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        List<Message> messages = messageService.selectByReceiveUserId(user.getId());
        return AppResult.success(messages);
    }

    @ApiOperation("更新为已读状态")
    @PostMapping("/markRead")
    public AppResult markRead(HttpServletRequest request,
                               @ApiParam("私信id") @RequestParam("id") Long id) {
        // 1. 判断私信是否存在
        Message message = messageService.selectById(id);
        if (message == null || message.getDeleteState() == 1) {
            return AppResult.failed("私信不存在");
        }
        // 2. 判断私信是否属于当前用户
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if (user.getId() != message.getReceiveUserId()) {
            return AppResult.failed(ResultCode.FAILED_FORBIDDEN);
        }
        // 3. 调用service
        if (message.getState() == 0) {
            // 未读状态变为已读状态
            messageService.updateStateById(id, (byte) 1);
        }
        return AppResult.success();
    }

    @ApiOperation("回复私信")
    @PostMapping("/reply")
    public AppResult reply(HttpServletRequest request,
                           @ApiParam("要回复的私信id") @RequestParam("repliedId") Long repliedId,
                           @ApiParam("回复内容") @RequestParam("content") String content) {
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        // 1. 判断当前登录用户是否被禁言
        if (user.getState() == 1) {
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        // 2. 判断私信是否存在
        Message existsMessage = messageService.selectById(repliedId);
        if (existsMessage == null || existsMessage.getDeleteState() == 1) {
            return AppResult.failed("私信不存在");
        }
        // 3. 判断私信是否属于当前用户
        if (user.getId() != existsMessage.getReceiveUserId()) {
            return AppResult.failed(ResultCode.FAILED_FORBIDDEN);
        }
        // 4. 构造回复对象
        Message message = new Message();
        message.setPostUserId(existsMessage.getReceiveUserId());
        message.setReceiveUserId(existsMessage.getPostUserId());
        message.setContent(content);
        // 5. 调用service
        messageService.reply(repliedId, message);
        // 6. 返回结果
        return AppResult.success();
    }
}
